﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Configuration;

namespace LibraryUtility.Web
{
    /// <summary>
    /// ClassName:PageValidate
    /// Auther:Leo.wl
    /// Time:2010-04-14
    /// 页面数据校验类
    /// </summary>
    public class PageValidate
    {
        private static Regex RegNumber = new Regex("^[0-9]*[1-9][0-9]*$");//^[0-9]+$
        private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
        private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
        private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
        private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串，和 [a-zA-Z0-9] 语法一样 
        private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");

        public PageValidate()
        {
        }

        //检查时候有危险字符
        /// <summary>
        /// Checks the param.
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <returns></returns>
        public static bool CheckParam(string str)
        {
            Regex r = new Regex("^\\w+$");
            Match m = r.Match(str);
            if (m.Success)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// Checks the date.
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <returns></returns>
        public static bool checkDate(string str)
        {

            Regex r = new Regex("^(\\d{4})\\-(\\d{2})\\-(\\d{2})$");
            Match m = r.Match(str);

            if (m.Success)
            {
                return true;
            }
            else
            {
                return false;

            }

        }


        /// <summary>
        /// Checks the in date.
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <returns></returns>
        public static bool checkInDate(string str)
        {
            Regex r = new Regex("^(\\d{4})\\-(\\d{2})\\-(\\d{2})$");
            Match m = r.Match(str);          


            if (m.Success)		//格式正确
            {
                string M = m.ToString();
                char[] da = new char[10];//char.Parse(m.ToString());

                M.CopyTo(0, da, 0, 10);

                bool bl = false;
                string yy = "";
                string mm = "";
                string dd = "";

                #region 分别取出 YYYY、MM、DD
                for (int i = 0; i < 4; i++)
                {
                    yy += da[i].ToString();
                }

                for (int i = 0; i < 2; i++)
                {
                    mm += da[5 + i].ToString();
                }

                for (int i = 0; i < 2; i++)
                {
                    dd += da[8 + i].ToString();
                }
                #endregion

                int YearLow = int.Parse(ConfigurationSettings.AppSettings["YearLow"]);
                int YearHigh = int.Parse(ConfigurationSettings.AppSettings["YearHigh"]);
                if (int.Parse(yy) < YearHigh && int.Parse(yy) > YearLow)		//
                {
                    #region  是否为有效的日期值
                    bool b = true;
                    if (b)
                    {
                        bool B = false;
                        switch (mm)
                        {
                            case "01": B = true;
                                break;
                            case "02": B = true;
                                break;
                            case "03": B = true;
                                break;
                            case "04": B = true;
                                break;
                            case "05": B = true;
                                break;
                            case "06": B = true;
                                break;
                            case "07": B = true;
                                break;
                            case "08": B = true;
                                break;
                            case "09": B = true;
                                break;
                            case "10": B = true;
                                break;
                            case "11": B = true;
                                break;
                            case "12": B = true;
                                break;
                        }
                        if (B)
                        {
                            switch (dd)
                            {
                                case "01": bl = true;
                                    break;
                                case "02": bl = true;
                                    break;
                                case "03": bl = true;
                                    break;
                                case "04": bl = true;
                                    break;
                                case "05": bl = true;
                                    break;
                                case "06": bl = true;
                                    break;
                                case "07": bl = true;
                                    break;
                                case "08": bl = true;
                                    break;
                                case "09": bl = true;
                                    break;
                                case "10": bl = true;
                                    break;
                                case "11": bl = true;
                                    break;
                                case "12": bl = true;
                                    break;
                                case "13": bl = true;
                                    break;
                                case "14": bl = true;
                                    break;
                                case "15": bl = true;
                                    break;
                                case "16": bl = true;
                                    break;
                                case "17": bl = true;
                                    break;
                                case "18": bl = true;
                                    break;
                                case "19": bl = true;
                                    break;
                                case "20": bl = true;
                                    break;
                                case "21": bl = true;
                                    break;
                                case "22": bl = true;
                                    break;
                                case "23": bl = true;
                                    break;
                                case "24": bl = true;
                                    break;
                                case "25": bl = true;
                                    break;
                                case "26": bl = true;
                                    break;
                                case "27": bl = true;
                                    break;
                                case "28": bl = true;
                                    break;
                                case "29": bl = true;
                                    break;
                                case "30": bl = true;
                                    break;
                                case "31": bl = true;
                                    break;
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    bl = false;
                }

                if (bl)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }

        }


        //检查url
        /// <summary>
        /// Checks the U rl.
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <returns></returns>
        public static bool checkURl(string str)
        {
            Regex r = new Regex("^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$");

            Match m = r.Match(str);
            if (m.Success)
            {
                return true;
            }
            else
            {
                return false;
            }


        }

        /// <summary>
        /// 非法字符转换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReplaceStr(string str)
        {
            //str=str.Replace(",","");
            str = str.Replace("-", "");
            str = str.Replace("'", "");
            str = str.Replace(";", "");
            str = str.Replace(":", "");
            str = str.Replace("/", "");
            str = str.Replace("?", "");
            str = str.Replace("<", "");
            str = str.Replace(">", "");
            str = str.Replace(".", "");
            str = str.Replace("#", "");
            str = str.Replace("%", "");
            str = str.Replace(" ", "");
            str = str.Replace("^", "");
            str = str.Replace("//", "");
            str = str.Replace("@", "");
            //			str=str.Replace("(","");
            //			str=str.Replace(")","");
            str = str.Replace("*", "");
            str = str.Replace("~", "");
            str = str.Replace("`", "");
            str = str.Replace("$", "");

            return str;


        }


        #region//对入库字符进行编码和转换。
        /// <summary>
        /// 对入库字符进行编码和转换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string EncodeStr(string str)
        {
            str = str.Replace("'", "’");
            str = str.Replace("\"", "&quot;");
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            str = str.Replace("\n", "<br>");
            return str;
        }
        #endregion

        #region 对出库字符进入显示时的转换。
        /// <summary>
        /// 对出库字符进行解码转换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DecodeStr(string str)
        {
            str = str.Replace("’", "'");
            str = str.Replace("&quot;", "\"");
            str = str.Replace("&lt;", "<");
            str = str.Replace("&gt;", ">");
            str = str.Replace("<br>", "\n");
            return str;
        }
        #endregion

        #region 数字字符串检查

        /// <summary>
        /// 检查Request查询字符串的键值，是否是数字，最大长度限制
        /// </summary>
        /// <param name="req">Request</param>
        /// <param name="inputKey">Request的键值</param>
        /// <param name="maxLen">最大长度</param>
        /// <returns>返回Request查询字符串</returns>
        public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
        {
            string retVal = string.Empty;
            if (inputKey != null && inputKey != string.Empty)
            {
                retVal = req.QueryString[inputKey];
                if (null == retVal)
                    retVal = req.Form[inputKey];
                if (null != retVal)
                {
                    retVal = SqlText(retVal, maxLen);
                    if (!IsNumber(retVal))
                        retVal = string.Empty;
                }
            }
            if (retVal == null)
                retVal = string.Empty;
            return retVal;
        }

        /// <summary>
        /// 是否数字字符串
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumber(string inputData)
        {
            Match m = RegNumber.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否数字字符串 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumberSign(string inputData)
        {
            Match m = RegNumberSign.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否是浮点数
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimal(string inputData)
        {
            Match m = RegDecimal.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否是浮点数 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimalSign(string inputData)
        {
            Match m = RegDecimalSign.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 中文检测

        /// <summary>
        /// 检测是否有中文字符
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsHasCHZN(string inputData)
        {
            Match m = RegCHZN.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 邮件地址
        /// <summary>
        /// 是否是浮点数 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsEmail(string inputData)
        {
            Match m = RegEmail.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 其他

        /// <summary>
        /// 检查字符串最大长度，返回指定长度的串
        /// </summary>
        /// <param name="sqlInput">输入字符串</param>
        /// <param name="maxLength">最大长度</param>
        /// <returns></returns>			
        public static string SqlText(string sqlInput, int maxLength)
        {
            if (sqlInput != null && sqlInput != string.Empty)
            {
                sqlInput = sqlInput.Trim();
                if (sqlInput.Length > maxLength)//按最大长度截取字符串
                    sqlInput = sqlInput.Substring(0, maxLength);
            }
            return sqlInput;
        }


        /// <summary>
        /// 字符串编码
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static string HtmlEncode(string inputData)
        {
            return HttpUtility.HtmlEncode(inputData);
        }

        /// <summary>
        /// 设置Label显示Encode的字符串
        /// </summary>
        /// <param name="lbl"></param>
        /// <param name="txtInput"></param>
        public static void SetLabel(Label lbl, string txtInput)
        {
            lbl.Text = HtmlEncode(txtInput);
        }

        /// <summary>
        /// Sets the label.
        /// </summary>
        /// <param name="lbl">The LBL.</param>
        /// <param name="inputObj">The input obj.</param>
        public static void SetLabel(Label lbl, object inputObj)
        {
            SetLabel(lbl, inputObj.ToString());
        }

        #endregion


        #region 获得<>中的内容
        /// <summary>
        /// Filters the HTM.
        /// </summary>
        /// <param name="htmlStr">The HTML STR.</param>
        /// <returns></returns>
        public static string filterHtm(string htmlStr)
        {
            int flag;
            if (htmlStr.IndexOf(">") < htmlStr.IndexOf("<") || htmlStr.IndexOf("<") == 0)
                flag = 0;
            else
                flag = 1;
            string filterStr = "";
            foreach (char str in htmlStr)
            {
                if (str.ToString() == "<")
                    flag = 0;
                if (flag == 1)
                    filterStr += str.ToString();
                if (str.ToString() == ">")
                    flag = 1;
            }
            return filterStr;
        }
        #endregion
    }
}
